package cn.hdu.liang.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class AtomicityTest implements Runnable {
    private long i = 0;

    public long getValue() {
        return i;
    }

    private synchronized void evenIncrement() throws InterruptedException {
        i++;
        i++;
    }

    @Override
    public void run() {
        while(true) {
            try {
                evenIncrement();
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        AtomicityTest at = new AtomicityTest();
        exec.execute(at);
        while(true) {
            long val = at.getValue();
            if(val % 2 != 0) {
                System.out.println(val);
                System.exit(0);
            }
        }
    }
}
